package com.jandar.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import com.jandar.Resources;

/**
 * 文件操作工具类。
 * 
 * @author pqj
 * @version 2009-6-2
 */
public class FileUtils {

	private boolean overrideFile = false;
	private String copyPrefix = null;
	private File infolder = null;
	private File outfolder = null;
	private File errfolder = null;
	private static final String ENCODEING = System.getProperty("file.encoding");
	private static FileUtils INSTANCE = new FileUtils();
	
	private FileUtils() {
		Resources config = Resources.getInstance();
		overrideFile = config.isOverrideFile();
		copyPrefix = config.getOverridePrefix();
		infolder = config.getInfolder();
		outfolder = config.getOutfolder();
		errfolder = config.getErrfolder();
	}

	public static FileUtils getInstance() {
		return INSTANCE;
	}
	
	/**
	 * 判断输入文件夹是否为空。
	 * 
	 * @return true: 空
	 */
	public boolean isInfolderEmpty() {
		if (infolder == null) {
			return true;
		}
		
		checkAndNewFile(infolder);
		
		return (infolder.list().length > 0) ? false : true;
	}
	
	/**
	 * 返回输入文件夹下的文件列表。
	 * 
	 * @return 文件列表
	 */
	public File[] fetchFiles() {
		if (isInfolderEmpty()) {
			return null;
		} else {
			return infolder.listFiles();
		}
	}
	
	/**
	 * 将文件复制到输出文件夹下。
	 * 
	 * @param srcFile 文件
	 * @return true: 成功
	 */
	public boolean copyFileToOutfolder(File srcFile) {
		return copyFile(srcFile, outfolder);
	}
	
	/**
	 * 将文件复制到错误文件夹下。
	 *  
	 * @param srcFile 文件
	 * @return true: 成功
	 */
	public boolean copyFileToErrfolder(File srcFile) {
		return copyFile(srcFile, errfolder);
	}
	
	/**
	 * 将文件移动到输出文件夹下。
	 * 
	 * @param srcFile 文件
	 * @return true: 成功
	 */
	public boolean removeFileToOutfolder(File srcFile) {
		boolean ret = false;
		ret = copyFileToOutfolder(srcFile);
		if (ret) {
			ret = deleteFile(srcFile);
		}
		return ret;
	}
	
	/**
	 * 将文件移动到错误文件夹下。
	 * 
	 * @param srcFile 文件
	 * @return true: 成功
	 */
	public boolean removeFileToErrfolder(File srcFile) {
		boolean ret = false;
		ret = copyFileToErrfolder(srcFile);
		if (ret) {
			ret = deleteFile(srcFile);
		}
		return ret;
	}

	public boolean copyFile(File srcFile, File desFile) {
		if (srcFile == null || desFile == null) {
			return false;
		}
		
		checkAndNewFile(desFile);
		
		boolean ret = false;
		FileInputStream fin = null;
		FileOutputStream fout = null;
		try {
			fin = new FileInputStream(srcFile);
			
			String desFileName = srcFile.getName();
			// 复制时候不覆盖
			if (!overrideFile) {
				desFileName = getDesFileName(desFileName, desFile);
			}
			
			fout = new FileOutputStream(new File(desFile, desFileName));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = fin.read(buf)) != -1) {
				fout.write(buf, 0, len);
			}
			ret = true;
		} catch (FileNotFoundException e) {
			ExceptionUtils.error(e);
		} catch (IOException e) {
			ExceptionUtils.error(e);
		} finally {
			if (fin != null) {
				try {
					fin.close();
				} catch (IOException e) {
					ExceptionUtils.error(e);
				}
			}
			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					ExceptionUtils.error(e);
				}
			}
		}
		
		return ret;
	}
	
	public boolean isOverrideFile() {
		return overrideFile;
	}

	public String getCopyPrefix() {
		return copyPrefix;
	}

	public File getInfolder() {
		return infolder;
	}

	public File getOutfolder() {
		return outfolder;
	}

	public File getErrfolder() {
		return errfolder;
	}
	
	public String getDesFileName(String srcFileName, File desFile) {
		String[] filesName = desFile.list();
		boolean has = false;
		for (String name : filesName) {
			if (name.equals(srcFileName)) {
				has = true;
				break;
			}
		}
		if (has) {
			return getDesFileName(copyPrefix + srcFileName, desFile); 
		} else {
			return srcFileName; 
		}
	}
	
	/**
	 * 删除文件。
	 * 
	 * @param delFile 文件
	 * @return true: 成功
	 */
	public static boolean deleteFile(File delFile) {
		if (delFile.isFile()) {
			return delFile.delete();
		}
		return false;
	}
	
	/**
	 * 递归创建文件或文件夹。
	 * 
	 * @param name 文件路径
	 * @param index 字符串长度
	 * @return true: 创建成功
	 */
	public static boolean createFile(String name, int index) {
		String name1 = name.substring(0, index);
		File file1 = new File(name1);
		boolean ret = false;
		if (file1.exists()) {
			return true;
		}
		int i;
		for (i=index - 1; i>1; --i) {
			if (name.charAt(i) == '/' || name.charAt(i) == '\\') {
				break;
			}
		}
		if (i > 1) {
			ret = createFile(name, i);
		}
		String name2 = name.substring(0, index);
		File file2 = new File(name2);
		if (!file2.exists()) {
			if (name.length() == index && name.indexOf('.') != -1) {
				try {
					ret = file2.createNewFile();
				} catch (IOException e) {
					ExceptionUtils.error(e);
				}
			} else {
				ret = file2.mkdir();
			}
		}
		return ret;
	}
	
	public static boolean checkAndNewFile(File f) {
		if (f != null && !f.exists()) {
			return createFile(f.getAbsolutePath(), f.getAbsolutePath().length());
		}
		return false; 
	}
	
	public static void writeFile(String file, String text) {
		
		checkAndNewFile(new File(file));
		
		if (text == null || "".equals(text)) {
			return ;
		}
		
		PrintStream print = null;
		try {
			print = new PrintStream(new FileOutputStream(file, true), true, ENCODEING);
			print.print(text);
		} catch (UnsupportedEncodingException e) {
			ExceptionUtils.error(e);
		} catch (FileNotFoundException e) {
			ExceptionUtils.error(e);
		} finally {
			if (print != null) {
				print.close();
			}
		}
	}
}
